package mipo.top.admin.service.impl;

import mipo.top.admin.Dto.empRole;
import mipo.top.admin.mapper.EmployeeMapper;
import mipo.top.users.domain.Logininfo;
import mipo.top.users.mapper.LogininfoMapper;
import mipo.top.admin.mapper.ShopMapper;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.admin.domain.Employee;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.base.util.MD5Utils;
import mipo.top.base.util.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceimpl extends BaseServiceimpl<Employee> implements IEmployeeService{
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void save(Employee employee) {
        String s = StrUtils.getComplexRandomString(32);
        String password = employee.getPassword();
        String md5 = MD5Utils.encrypByMd5(s + password);
        employee.setSalt(s);
        employee.setPassword(md5);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfoMapper.save(logininfo);
        employee.setLogininfoId(logininfo.getId());
        super.save(employee);
        empRole empRole = new empRole();
        empRole.setEmployee_id(employee.getId());
        init(employee);
    }
    private void init(Employee employee){
        employeeMapper.removeEmpRole(employee.getId());
        employeeMapper.saveEmpRole(employee.getId(),employee.getRoles());
    }
    @Override
    public void update(Employee employee) {
        String s = StrUtils.getComplexRandomString(32);
        String password = employee.getPassword();
        String md5 = MD5Utils.encrypByMd5(s + password);
        employee.setSalt(s);
        employee.setPassword(md5);
        super.update(employee);
        Long logininfoId = employee.getLogininfoId();
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setId(logininfoId);
        logininfo.setType(0);
        logininfoMapper.update(logininfo);
        empRole empRole = new empRole();
        empRole.setEmployee_id(employee.getId());
        init(employee);
    }
    public void remove(Long id) {
        Employee findone = super.findone(id);
        Long shopId = findone.getShopId();
        if(findone.getLogininfoId()!=null) {
            logininfoMapper.remove(findone.getLogininfoId());
        }
        if(shopId!=null){
            shopMapper.remove(shopId);
        }
        super.remove(id);
    }

    @Override
    public Employee findBylogininfoId(Long id) {
        return employeeMapper.findBylogininfoId(id);
    }

    @Override
    public List<Employee> findByshopId(Long shopId) {
        return employeeMapper.findByshopId(shopId);
    }
}
